package org.dfl.core.data;

/**
 * Class to represent token objects. Each token keeps its data and history. 
 * Token can be active or disabled. If token is disabled then it's not visible
 * for transitions in a flow.
 * 
 * Each token has its own unique id generated when created.
 * @author Piotr Wlodarczyk
 *
 */
public class Token {
	/** id for next token **/
	static int nextId = 1;	
	
	/** history of this token **/
	private History history;
	/** data object **/
	private Object data;
	/** token id **/
	private int id = 0;
	/** active == true then token is visible for transitions **/
	private boolean active = true;
	
	/**
	 * create token with empty history
	 * @param data - token's data object
	 */
	public Token(Object data) {
		super();
		this.data = data;
		this.history = new History();
		this.id = Token.nextId++;
	}
	
	/**
	 * create token with given data and history
	 * @param data - token data object
	 * @param history - token's history
	 */
	public Token(Object data, History history) {
		super();
		this.history = history;
		this.data = data;
		id = Token.nextId++;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
	
	public void setHistory(History history) {
		this.history = history;
	}

	public History getHistory() {
		return history;
	}
	
	@Override
	public String toString(){
		return "<HD><data>"+getData()+"</data> "+getHistory()+"</HD>";
	}
	
	public int getId(){
		return id;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
	
	public Token getCopy(){
		Token t = new Token(getData());
		t.setActive(isActive());
		t.setHistory(getHistory().getCopy());
		return t;
	}
}
